package gui;

import java.util.List;

import negocio.QuestaoNegocio;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import beans.Alternativa;
import beans.Questao;

import com.example.prova_concursos.R;

public class QuestaoActivity extends ActionBarActivity {

	ListView listView;
	List<Questao> listaQuestoes;
	private int pagina = -1;
	List<Alternativa> listAlternativas;
	private long idServico;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_questao);
		
		Intent intent = getIntent();
		Bundle dados = intent.getExtras();
		this.idServico = dados.getLong("id");

		pegarQuestoes();
		View v = findViewById(R.layout.activity_questao);
		proximaPergunta(v);
	}

	public void proximaPergunta(View view) {
		pagina++;
		mostraQuestao();
		
		


	}

	public void ultimaPergunta(View view) {
		pagina--;
		mostraQuestao();
		


	}

	private void mostraQuestao() {
		Questao questaoAtual = listaQuestoes.get(pagina);
		listAlternativas = questaoAtual.getListAlternativa();

		TextView enunciado = (TextView) findViewById(R.id.textViewEnunciado);
		enunciado.setText(Html.fromHtml(questaoAtual.getEnunciado()));

		Log.d("ALTERNATIVA", String.valueOf(listAlternativas.size()));

		listView = (ListView) findViewById(R.id.listViewListAlternativas);
		ArrayAdapter<Alternativa> adapter = new MyListAdapter();
		listView.setAdapter(adapter);
	}
	private class MyListAdapter extends ArrayAdapter<Alternativa> {

		public MyListAdapter() {
			super(QuestaoActivity.this, R.layout.list_item_alternativas,
					listAlternativas);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View itemView = convertView;

			if (itemView == null) {
				itemView = getLayoutInflater().inflate(
						R.layout.list_item_alternativas, parent, false);
			}
			Alternativa currentAlternativa = listAlternativas.get(position);

			TextView alternativa = (TextView) itemView
					.findViewById(R.id.textViewAlternativa);
			alternativa.setText(Html.fromHtml(currentAlternativa.getDescricao()));

			return itemView;
		}

	}

	public void pegarQuestoes() {

		QuestaoNegocio questoes = new QuestaoNegocio();
		listaQuestoes = questoes.listaQuestoes(idServico, this);

	}
}
